package com.mef.smsplat.eterm;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.mef.smsplat.eterm.util.EtermUtil;

/**
 * 指令抽象类
 * @author hc24
 *
 */
public abstract class AComm {
	protected static final Log logger=LogFactory.getLog(AComm.class);
	
	private static byte[] b13 = {13};
	private static String s13 = new String(b13);
	private static byte[] b10 = {10};
	private static String s10 = new String(b10);
	private static byte[] b28 = {28};
	private static String s28 = new String(b28);
	private static byte[] b29 = {29};
	private static String s29 = new String(b29);
	private static byte[] b30 = {30};
	private static String s30 = new String(b30);
	private static byte[] b03 = {03};
	private static String s03 = new String(b03);
	
	public static String spliter=s13;
	
	/** 一些看不清楚的SB字符 */
	protected static List<String> sb=new ArrayList<String>();
	
	private IEterm eterm;

	static{
		//sb.add(s13);
		//sb.add(s10);
		sb.add(s28);
		sb.add(s29);
		sb.add(s30);
		sb.add(s03);
	}
	
	public IEterm getEterm() {
		return eterm;
	}

	public void setEterm(IEterm eterm) {
		this.eterm = eterm;
	}
	
	public String sendCmd(String cmd){
		if(eterm==null)
			throw new RuntimeException("Eterm对象为空!");
		String res=null;
		try {
			//eterm.connect();//建立连接
			res=eterm.send(cmd);
			/* 去掉SB字符 */
			res=EtermUtil.dealResult(res);
//			for(int i=0;i<sb.size();i++){
//				res=res.replaceAll(sb.get(i), "");
//			}
		} catch (Exception e) {
			logger.error("发送或接受指令出错",e);
		}finally{
//			try {
//				eterm.close();
//			} catch (Exception e) {
//				logger.error(e);
//			}//关闭
		}
		return res;
		
	}
	
	
}
